package unclassified.medium;

public class DecodeTheSlantedCiphertext_5828 {

    public String decodeCiphertext(String encodedText, int rows) {
        int width = encodedText.length() / rows;
        // 0  1   2   3
        // 4  5   6   7
        // 8  9  10  11
        StringBuilder sb = new StringBuilder();
        if (encodedText.length() == 0) {
            return sb.toString();
        }
        for (int i = 0; i < width; i++) {
            int j = 0;
            int index = j * width + i + j;
            while(index < encodedText.length()) {
                sb.append(encodedText.charAt(index));
                j++;
                index = j * width + i + j;
            }
        }
        while(sb.charAt(sb.length() - 1) == ' ') {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
